﻿using Aribaccio;
using Aribaccio.Device;
using BDPAutodetect.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BDPAutodetect.Simcore
{
    public class DeviceSimulatorBase
    {
        /// <summary>
        /// 
        /// </summary>
        public event Action<string>? MessageIn;
        /// <summary>
        /// 
        /// </summary>
        public event Action<string>? MessageOut;
        /// <summary>
        /// 
        /// </summary>
        public event Action<DeviceCommandData>? DeviceCommandDataActive;

        /// <summary>
        /// 设备指令管理实例
        /// </summary>
        protected DeviceCommandManager DeviceCommandMgr { get; set; }

        /// <summary>
        /// 唯一标识码
        /// </summary>
        public virtual Guid GUID { get; set; }
        /// <summary>
        /// 是否是唯一的项
        /// </summary>
        public virtual bool IsSingleItem => false;
        /// <summary>
        /// 是否打开
        /// </summary>
        public virtual bool IsOpen { get; }
        /// <summary>
        /// 设备地址端口
        /// </summary>
        public virtual string DeviceAddressPort { get; }

        /// <summary>
        /// 设备名称
        /// </summary>
        public string DeviceName { get; set; }
        /// <summary>
        /// 设备类型
        /// </summary>
        public DeviceType DeviceType { get; protected set; }
        /// <summary>
        /// 设备模组类型
        /// </summary>
        public DeviceModule DeviceModule { get; protected set; }
        /// <summary>
        /// 设备指令数据
        /// </summary>
        public List<DeviceCommandData> DeviceCommandDatas { get; protected set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        protected DeviceSimulatorBase(string argDeviceName, DeviceType argDeviceType, DeviceModule argDeviceModule)
        {
            this.GUID = Guid.NewGuid();
            this.DeviceName = argDeviceName;
            this.DeviceType = argDeviceType;
            this.DeviceModule = argDeviceModule;
            this.DeviceAddressPort = string.Empty;
            this.DeviceCommandMgr = new DeviceCommandManager();
            this.DeviceCommandDatas = new List<DeviceCommandData>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void Initialize()
        {
            this.DeviceCommandDatas = this.DeviceCommandMgr.GetDeviceCommandDatas(this.DeviceType.ToStringName());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="argMessage"></param>
        protected void SendMessageIn(string argMessage)
        {
            MessageIn?.Invoke(argMessage);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="argMessage"></param>
        protected void SendMessageOut(string argMessage)
        {
            MessageOut?.Invoke(argMessage);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="argCommandData"></param>
        protected void OnActiveDeviceCommandData(DeviceCommandData argCommandData)
        {
            DeviceCommandDataActive?.Invoke(argCommandData);
        }

        /// <summary>
        /// 获取设备指令数据
        /// </summary>
        /// <returns></returns>
        protected DeviceCommandData? GetActiveDeviceCommandData(string argTargetCommand)
        {
            if (this.DeviceCommandDatas == null || this.DeviceCommandDatas.Count <= 0) return default;
            return this.DeviceCommandDatas.FirstOrDefault(v => argTargetCommand.StartsWith(v.TargetCommand, StringComparison.OrdinalIgnoreCase));
        }
    }
}
